`timescale  1ns / 1ps   

module tb_EnorDecode;   

// EnorDecode Parameters
parameter PERIOD  = 10; 


// EnorDecode Inputs
reg   clk                                  = 1 ;
reg   wrregen                              = 0 ;
reg   mixsel                               = 0 ;
reg   reginsel                             = 0 ;
reg   en_de                                = 1 ;
reg   [1:0]  keyadsel                      = 0 ;
reg   [127:0]  intxt                       = 0 ;
reg   [127:0]  roundkey                    = 0 ;

// EnorDecode Outputs
wire  [127:0]  outtxt                      ;


initial
begin
    forever #(PERIOD/2)  clk=~clk;
end

EnorDecode  u_EnorDecode (
    .clk                     ( clk               ),
    .wrregen                 ( wrregen           ),
    .mixsel                  ( mixsel            ),
    .reginsel                ( reginsel          ),
    .en_de                   ( en_de             ),
    .keyadsel                ( keyadsel  [1:0]   ),
    .intxt                   ( intxt     [127:0] ),
    .roundkey                ( roundkey  [127:0] ),

    .outtxt                  ( outtxt    [127:0] )
);

initial
begin
    //decryption.
    #(PERIOD*2+1);
	wrregen=1;
	keyadsel=2'b00;
	mixsel=0;
	reginsel=0;
	intxt=128'h69c4e0d86a7b0430d8cdb78070b4c55a;
		                      
    #(PERIOD); 
	mixsel=1;
	reginsel=1;
		     
	#(PERIOD*9);

    keyadsel=2'b11;
    mixsel=0;
    reginsel=0;	                
    #(PERIOD)wrregen=0;

    #(PERIOD*10);	
    $stop;
end

integer fkd,fte,ftd,i=0;
reg [127:0]mem [0:10];
initial begin
    #(PERIOD*2+1);

    fkd = $fopen("./data/key_de.info","r");
    fte = $fopen("./data/encoen_txt.info","r");
    ftd = $fopen("./data/decode_txt.info","w");

    while((!$feof(fkd)))begin
        $fscanf(fkd,"%h",roundkey);
        #(PERIOD);
        $fwrite(ftd,"%2d %h\n",i,outtxt);i=i+1;
    end

    $fclose(fkd);
    $fclose(fte);
    $fclose(ftd);
    $stop;
end

endmodule